package com.aiapphub.SystemCode.controller;
 
 

import com.alibaba.excel.EasyExcel;
import com.aiapphub.BaseCode.common.ResponseResult;
import com.aiapphub.BaseCode.constants.AppHttpCodeEnum;
import com.aiapphub.BaseCode.exception.SystemException;
import com.aiapphub.BaseCode.vo.PageVo;
import com.aiapphub.SystemCode.easyExcel.listeners.SalaryListener;
import com.aiapphub.SystemCode.easyExcel.model.importExcel.SalaryImportExcel;
import com.aiapphub.SystemCode.entity.FileInfo;
import com.aiapphub.SystemCode.entity.SalaryStatement;
import org.springframework.beans.factory.annotation.Autowired;
import com.aiapphub.SystemCode.service.SalaryService;
import org.springframework.web.bind.annotation.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * 工资明细表(SalaryStatement)表控制层
 *
 * @author makejava
 * @since 2025-04-19 15:53:12
 */
@RestController
@Slf4j
@CrossOrigin
@RequestMapping("/salary")
public class SalaryController {
    /**
     * 服务对象
     */
    @Autowired
    private SalaryService salaryService;


    /**
     * 通过主键查询单条数据
     *
     * @return 单条数据
     */
    @GetMapping("/statement/{accountPeriod}/{jobNumber}")
    public ResponseResult getSalaryStatement(
            @PathVariable String accountPeriod,
            @PathVariable String jobNumber) {
        SalaryStatement salaryStatement = salaryService.getSalaryStatement(accountPeriod, jobNumber);
        System.out.println(salaryStatement);
        return ResponseResult.okResult(salaryStatement);
    }


    /**
     * 分页查询
     * @param pageNum 查询页数
     * @param pageSize 每页大小
     * @return
     */
    @GetMapping("/page/{pageNum}/{pageSize}")
    public ResponseResult getPageInfo(@PathVariable Integer pageNum, @PathVariable Integer pageSize) {
        PageVo pageVo = salaryService.selectSalaryPage(pageNum, pageSize);
        return ResponseResult.okResult(pageVo);
    }

    /***
     * 根据条件查询
     * @param salaryStatement
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/list")
    public ResponseResult list(SalaryStatement salaryStatement, Integer pageNum, Integer pageSize){
        PageVo pageVo =  salaryService.selectCategoryPage(salaryStatement,pageNum,pageSize);
        return ResponseResult.okResult(pageVo);
    }


    /**
     * 删除数据
     *
     * @param id 主键结合
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    public ResponseResult delete(@PathVariable("id") Long id) {
        salaryService.removeById(id);
        return ResponseResult.okResult();
    }

    /**
     * 根据账期修改状态
     *
     * @param accountPeriod 账期
     * @param status 状态值
     * @return 更新结果
     */
    @PutMapping("/updateStatus/{accountPeriod}/{status}")
    public ResponseResult updateStatus(
            @PathVariable String accountPeriod,
            @PathVariable Integer status) {

        if (salaryService.updateSalaryStatus(accountPeriod, status)) {
            return ResponseResult.okResult("状态更新成功");
        }else {
            return ResponseResult.errorResult(AppHttpCodeEnum.UPDATE_FAIL);
        }

    }


    @PostMapping("/batchImport")
    public ResponseResult batchImport (@RequestParam("file") MultipartFile file) {

        try {
            // 判断文件类型是否正确
            String originalFilename = file.getOriginalFilename();
            String fileType = originalFilename.substring(originalFilename.lastIndexOf("."));
            if (!".xls".equalsIgnoreCase(fileType) && !".xlsx".equalsIgnoreCase(fileType)) {
                throw new SystemException(AppHttpCodeEnum.FORMAL_ERROR);
            }
            // 解析第一个 sheet 页
            EasyExcel.read(file.getInputStream(), SalaryImportExcel.class, new SalaryListener()).headRowNumber(2).sheet().doRead();
            return ResponseResult.okResult();
        } catch (IOException e) {
            e.printStackTrace();
            throw  new SystemException(AppHttpCodeEnum.INTERNAL_SERVER_ERROR);
        }
    }





 

}
